package com.longy.common.utils;

import java.util.concurrent.Callable;

public class Retryer {

    public static <T> T retry(Callable<T> method, int[] delays) throws Exception {
        int maxRetries = 0;
        for (int delay : delays) {
            try {
                return method.call();
            } catch (Exception e) {

                    System.out.println("Attempt " + (maxRetries + 1) + " failed. Retrying in " + delays[maxRetries] + " seconds...");
                    Thread.sleep(delays[maxRetries] * 1000);
                maxRetries++;
            }
        }
        return null;
    }

    public static void main(String[] args) {
        int[] delays = {5, 10, 20}; // delays in seconds

        try {
            String result = retry(() -> {
                // Replace with the actual method call
                if (Math.random() < 0.7) {
                    throw new Exception("Failed");
                }
                return "Success";
            }, delays);
            System.out.println("Result: " + result);
        } catch (Exception e) {
            System.out.println("Final result: " + e.getMessage());
        }
    }
}